<template>
	<view :class="['page',{'iphoneX':isIphoneX}]">  
		<view class="body"> 
			<template >
				<view class="goods-wrap" >					 	
					<u--form :model="form" ref="uForm" class="form-wrap"> 
						<u-form-item label="申请用户" labelWidth="160" prop="user_name" borderBottom="true" class="u-form-item" @click="users.isShow=true">
							<u-input v-model="form.user_name" placeholder="申请用户" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<u-form-item label="结清状态" labelWidth="160" prop="project_statusname" borderBottom="true" class="u-form-item" @click="project_status.isShow=true">
							<u-input v-model="form.project_statusname" placeholder="选择结清状态" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item> 
						<u-form-item label="客户或手机" labelWidth="180" prop="unit" borderBottom="true" class="u-form-item">
							<u-input v-model="form.phone" type="text" class="text-inp" border="none" placeholder="输入客户或手机"/> 
						</u-form-item>
						<u-form-item label="选择仓库" labelWidth="160" prop="storehouse_name" borderBottom="true" class="u-form-item" @click="storehouse.isShow=true;">
							<u-input v-model="form.storehouse_name" placeholder="选择仓库" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item> 
						<u-form-item label="货物类别" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item" @click="pcategory.isShow=true">
							<u-input v-model="form.p_name" placeholder="选择货物类别" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item> 
						<u-form-item label="货物名称" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item" @click="pnames.isShow=true">
							<u-input v-model="form.pname" placeholder="选择货物名称" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item> 
						<u-form-item label="签收状态" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item" @click="signs.isShow=true">
							<u-input v-model="form.sign_name" placeholder="选择签收状态" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item> 		
						<view class="row-2">
							<u-form-item label="开始时间" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item" @click="show=true;date_type=1">
								<u-input v-model="form.starttime"  placeholder="选择开始时间" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;" />														
							</u-form-item> 
							<u-icon v-if="form.starttime" slot="right" name="close-circle-fill" @click="form.starttime=''"></u-icon>
							<u-icon	v-else slot="right" name="arrow-right" color="#ff0000" ></u-icon>	
						</view>	
						<view class="row-2">
							<u-form-item label="结束时间" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item" @click="show=true;date_type=2">
								<u-input v-model="form.endtime" placeholder="选择结束时间" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;" />														
							</u-form-item>
							<u-icon v-if="form.endtime" slot="right" name="close-circle-fill" @click="form.endtime=''"></u-icon>
							<u-icon	v-else slot="right" name="arrow-right" color="#ff0000"></u-icon>		
						</view>					
						
						
					</u--form>
				</view>				
			     
			
			</template>
			 
			<u-picker :show="project_status.isShow" :columns="project_status.data" keyName="name" itemHeight="90" :closeOnClickOverlay="true" @cancel="project_status.isShow=false" @confirm="changeprojectstatus" @close="project_status.isShow=false"></u-picker>
			
			<u-picker :show="signs.isShow" :columns="signs.data" keyName="name" itemHeight="90" :closeOnClickOverlay="true" @cancel="signs.isShow=false" @confirm="changesignstatus" @close="signs.isShow=false"></u-picker>
			
			<u-picker :show="storehouse.isShow" :columns="storehouse.data" keyName="sname" itemHeight="90" :closeOnClickOverlay="true" @cancel="storehouse.isShow=false" @confirm="changestorehouse" @close="storehouse.isShow=false"></u-picker>
			
			<u-picker :show="pcategory.isShow" :columns="pcategory.data" keyName="name" :defaultIndex="pcategory.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="pcategory.isShow=false" @confirm="changepcategory" @close="pcategory.isShow=false"></u-picker>
			
			<u-picker :show="pnames.isShow" :columns="pnames.data" keyName="pname" :defaultIndex="pnames.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="pnames.isShow=false" @confirm="changepnames" @close="pnames.isShow=false"></u-picker>
			 
			<u-picker :show="users.isShow" :columns="users.data" keyName="name" :defaultIndex="users.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="users.isShow=false" @confirm="changeusers" @close="users.isShow=false"></u-picker>  
			
			<u-datetime-picker :show="show" v-model="date" itemHeight="90" mode="date" @cancel="show=false" @confirm="changeDate"></u-datetime-picker>
			 
		</view>  
		<c-footer > 
			<view slot="content" class="add-btn" @click="checkFileStorage">导出数据</view> 			
		</c-footer>
	</view>
</template>

<script>
	import {uploadToServe,dateFormat} from '@/common/util.js'
	import {IMG_PATH,HTTP_REQUEST_URL} from '../../config.js'
	export default {
		data() {
			return {
				id:'',
				isLogin:true, 
				isIphoneX:getApp().globalData.isIphoneX, 
				systemInfo:getApp().globalData.systemInfo,
				userInfo:uni.getStorageSync('userInfo'),
				isShowSearch:false,
				type:'', 
				page:'',
				list:[], 
				form: { 	
					user_name:'',
					user_id:'',
					pname:'',
					pname_id:'',
					p_id:'', 
					p_name:'',
					storehouse_name:'',
					storehouse_id:'',
					storehouse_uid:'',    
					project_statusid:'',
					project_statusname:'',
					starttime:'',
					endtime:'',
					phone:'',
					text:'',
					sign:'',
					sign_name:''
				},
				signs:{
					isShow:false,
					data:[
						[
							{
								id:0,
								name:'未签收'
							},
							{
								id:1,
								name:'已签收'
							}
						]
					]
				},
				project_status:{
					isShow:false,
					data:[[
						{
							value:'',
							name:'不限'
						},
						{
							value:1,
							name:'未结清'
						},
						{
							value:2,
							name:'已结清'
						},
						{
							value:3,
							name:'部分结清'
						}
					]],
					defaultIndex:[]
				},	
				pcategory:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				pnames:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},				 
				storehouse:{
					isShow:false,
					data:[],
					defaultIndex:[]
				},	
				users:{
					isShow:false,
					data:[],
					defaultIndex:[]
				},
				show: false,
				date: Number(new Date()),
				date_type:1,
				auth:'',
				filename:''
			}
		},
		async onLoad(params) {   
			console.log(this.userInfo)
			this.productstorehouse();
			this.productcategorylist();  
			this.goodsexportalluser();
		},
		onShow() {
			
		}, 
		methods: {  
			async getauth(){
				let res=await this.$api.user_api.getauth();  
				if(res.data.code==200){    
					this.auth=res.data.data.join(','); 
				}
			}, 
			changesignstatus(e){ 
				this.form.sign_name=e.value[0].name;
				this.form.sign=e.value[0].id;
				this.signs.isShow=false;
			},
			changeDate(e){ 
				if(this.date_type==1){
					this.form.starttime=dateFormat(e.value,'YYYY-MM-DD');
				}
				if(this.date_type==2){
					this.form.endtime=dateFormat(e.value,'YYYY-MM-DD');
				}
				this.show=false;
			},
			
			changeprojectstatus(e){
				this.form.project_statusid=e.value[0].value;
				this.form.project_statusname=e.value[0].name;
				this.project_status.isShow=false;
			},
			
			async productstorehouse(){
				let res=await this.$api.user_api.productstorehouse();  
				if(res.data.code==200){ 
					res.data.data.unshift({
						storehouseman:'',
						sname:'不限'
					})
					this.$set(this.storehouse.data,0,res.data.data); 
					// this.form.storehouse_id=res.data.data[0].id;
					// this.form.storehouse_name=res.data.data[0].sname;
					// this.form.storehouse_uid=res.data.data[0].storehouseman; 
				}
			}, 
			
			changeusers(e){
				this.form.user_name=e.value[0].name;
				this.form.user_id=e.value[0].id;  
				this.users.isShow=false;
				this.storehouse.defaultIndex=e.indexs;  
			},
			
			async goodsexportalluser(){
				let auth=await this.$api.user_api.getauth(); 
				if(auth.data.code==200){
					let res=await this.$api.user_api.goodsexportalluser();  
					console.log(auth.data.data)
					if(auth.data.data.join(',').indexOf('21')==-1){
						res.data.data.forEach(item=>{
							if(item.id==this.userInfo.id){
								this.$set(this.users.data,0,[item]); 
								this.form.user_name=item.name;
								this.form.user_id=item.id;  
							}else{
								this.$set(this.users.data,0,[{
									"id": this.userInfo.id,
									"name": this.userInfo.username
								}]);
								this.form.user_name=this.userInfo.username;
								this.form.user_id=this.userInfo.id;  
							}
						})
					}else{
						this.$set(this.users.data,0,res.data.data); 
					}
				}
			}, 
			
			changestorehouse(e){ 
				this.form.storehouse_name=e.value[0].sname;
				this.form.storehouse_id=e.value[0].storehouseman; 
				this.form.storehouse_uid=e.value[0].storehouseman;
				this.storehouse.isShow=false;
				this.storehouse.defaultIndex=e.indexs;  
			},
			 	 
			
			async getFilename(){
				let {user_id,pname_id,p_id,storehouse_id,project_statusid,starttime,endtime,phone,text,sign}=this.form;				 
				 
				let params={
					user_id, 
					storehouse_id,
					project_status:project_statusid,
					product_id:pname_id,
					category_id:p_id,
					starttime,
					endtime,
					phone,
					text,
					sign
				} 
				console.log(params) 
				let res=await this.$api.user_api.goodsexport(params);  
				if(res.data.code==200){
					this.filename=res.data.pdata;
					this.downLoad(); 	
				}
				
				 
			},
			
			// 检测文件存储是否开启
			checkFileStorage(){
				let _this = this;
				plus.android.requestPermissions(['android.permission.WRITE_EXTERNAL_STORAGE'], function(e) {  
					// 权限被永久拒绝
					if (e.deniedAlways.length > 0) { 
						// 弹出提示框解释为何需要读写手机储存权限，引导用户打开设置页面开启
						uni.showModal({
							title: '存储权限',
							content: '您拒绝了存储权限，请去设置-应用开启存储权限。',
							success: function (res) {
								if (res.confirm) {
									// console.log('用户点击确定');
								} else if (res.cancel) {
									// console.log('用户点击取消');
								}
							}
						});
					}
					
					// 权限被临时拒绝
					if (e.deniedPresent.length > 0) { 
						// 弹出提示框解释为何需要读写手机储存权限，可再次调用plus.android.requestPermissions申请权限
						plus.android.requestPermissions(['android.permission.WRITE_EXTERNAL_STORAGE'])
						// console.log('666666666 ' + e.deniedPresent.toString());
					}
					
					// 权限已打开
					if (e.granted.length > 0) { 
						console.log('可以下载') 
						_this.getFilename();
						// _this.downLoad();
					}
				}, function(e) {
					// console.log('R12133313221' + JSON.stringify(e));
				}); 
			},
			
			// 下载文件
			downLoad(){				 
				let path='';
				path=`${HTTP_REQUEST_URL}/api/image/export?path=${this.userInfo.id}&filename=${this.filename}` 
				uni.showLoading({
					title: "下载中...",
					mask:true
				})  
				let dtask = plus.downloader.createDownload(path, {
					filename: "file://storage/emulated/0/瑞丰生物/" + this.filename       //利用保存路径，实现下载文件的重命名
				},(d, status)=> {
					//d为下载的文件对象 
					if (status == 200) {
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '文件已保存', //保存路径
							duration: 3000,
						});
						//下载成功,d.filename是文件在保存在本地的相对路径，使用下面的API可转为平台绝对路径
						let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename); 
					} else {
						//下载失败
						uni.hideLoading();
						plus.downloader.clear(); //清除下载任务
						uni.showToast({
							icon:'none',
							mask:true,
							title: '下载失败，请稍后重试',
						});
					}
				})
				dtask.start(); 		 
				
			},
			  
			
			changepnames(e){
				let idx=this.form.goodsIndex;
				this.form.pname=e.value[0].pname;
				this.form.pname_id=e.value[0].id; 
				this.pnames.isShow=false; 
			},
			 
			 
			changepcategory(e){   
				this.form.p_id=e.value[0].id;
				this.form.p_name=e.value[0].name; 
				this.pcategory.isShow=false;
				if(this.form.p_id==''){
					this.form.pname='不限';
					this.form.pname_id=''; 
				}else{
					this.form.pname='';
					this.form.pname_id=''; 
				}
				
				this.productalllist(this.form.p_id);
			},
			
			async productcategorylist(){
				let res=await this.$api.user_api.outallprotype();   
				if(res.data.code==200){  
					res.data.data.unshift({
						id:'',
						name:'不限'
					})
					this.$set(this.pcategory.data,0,res.data.data);   
				}
			},  
			 
			
			async productalllist(cid){
				let res=await this.$api.user_api.productalllist({
					categoryid:cid,
					uid:''
				});  
				
				if(res.data.code==200){  
					res.data.data.unshift({
						id:'',
						pname:'不限'
					})
					this.$set(this.pnames.data,0,res.data.data); 				
					 
				}
			}, 
			
			// async productalllist(cid){
			// 	let res=await this.$api.user_api.partslist({
			// 		typeid:cid,
			// 		is_page:''
			// 	});      
			// 	if(res.data.code==200){  
			// 		if(res.data.data.list.length>0){
			// 			this.$set(this.pnames.data,0,res.data.data.list);
			// 			let idx=this.form.goodsIndex;  
			// 		}					
			// 	}
			// }, 
			 
			 
		},
		computed:{
			 
			 
		},
		watch:{
			 
		}
	}
</script>

<style lang="less" >
	.hd-title{
		display: flex;
		justify-content: space-between;
		padding: 20rpx;
	}
	.upload-wrap{
		padding:20rpx;		
		background: #fff;
	}
	.u-form-item{
		.text-inp{
			width: 160rpx;
		}
		.text-unit{
			width: 180rpx;
		}
	}
	.form-wrap{
		padding:0 20rpx; 
		.row-2{
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}
	.slot-content{
		.hd{
			padding: 30rpx 0 10rpx;
			font-size: 30rpx;
		}
	}
	.popup-form-wrap{
		padding:20rpx;
		border-radius: 15rpx;		 
	}
	.select-pz-wrap{
		display: flex;
		flex-wrap: wrap;
		.item{
			position: relative; 
			margin:0 20rpx 20rpx 0;
			.del-icon{
				position: absolute;
				right: -15rpx;
				top: -15rpx;
			}
		}
	}
	.add-btn{
		height: 36px;
		line-height: 36px;
		width: 96%;
		margin: 5px auto;
		background: #1cab1b;
		border-radius: 8rpx;
		color: #fff;
		font-size: 30rpx;
		text-align: center;
	}  
	.partslist-popup{ 
		position: relative;
		height: 100%;
		padding: 0 40rpx; 
		.hd{ 
			height: 40px;
		}
		.select-wrap{
			height: 80%;
			flex-grow: 2;
			margin: 20rpx 0;
			overflow-y: auto; 
			.item{
				margin-bottom: 20rpx;
			}
		}
		.btn-wrap{  
			margin: 20rpx 0;
			.btn{
				margin: 0 30rpx;
			}
		}
	}
</style>
